package com.linchong.concurrency.chapter10;

/**
 * @Author:linchong
 * @CreateTime:2020-07-28 10:08
 * @Description: 演示synchronized加锁
 */
public class SynchronizedProblem {

	public static void main(String[] args) throws InterruptedException {
		new Thread(){
			@Override
			public void run() {
				SynchronizedProblem.run();
			}
		}.start();

		//t1执行，除非t1工作完成，t2才有 机会去执行
		Thread t2 = new Thread() {
			@Override
			public void run() {
				SynchronizedProblem.run();
				//t2中还有其他的工作要处理，释放它需要，不想等待，
			}
		};
		t2.start();
		//睡眠2s,如果此时t1未运行结束，
		Thread.sleep(2_000);
		t2.interrupt();
		//输出t2状态
		System.out.println(t2.isInterrupted());
	}

	private synchronized static void run(){
		System.out.println(Thread.currentThread());
		while (true){

		}
	}
}
